﻿using System;
using System.ComponentModel.DataAnnotations;
using JetBrains.Annotations;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.MultiTenancy;

namespace PermissionManagement;

[Index(nameof(TenantId))]
[Index(nameof(ProviderName), nameof(ProviderKey), nameof(ModuleName), nameof(FunctionName))]
public class PermissionGrant : Entity<Guid>, IMultiTenant
{
    public virtual Guid? TenantId { get; protected set; }

    [NotNull]
    [MaxLength(128)]
    public virtual string ModuleName { get; protected set; }

    [NotNull]
    [MaxLength(32)]
    public virtual string FunctionName { get; protected set; }

    [NotNull]
    [MaxLength(32)]
    public virtual string ProviderName { get; protected set; }

    [CanBeNull]
    [MaxLength(64)]
    public virtual string ProviderKey { get; protected internal set; }

    protected PermissionGrant()
    {

    }

    public PermissionGrant(
        Guid id,
        [NotNull] string moduleName,
        [NotNull] string functionName,
        [NotNull] string providerName,
        [CanBeNull] string providerKey,
        Guid? tenantId = null)
    {
        Id = id;
        ModuleName = Check.NotNullOrWhiteSpace(moduleName, nameof(moduleName));
        FunctionName = Check.NotNullOrWhiteSpace(functionName, nameof(functionName));
        ProviderName = Check.NotNullOrWhiteSpace(providerName, nameof(providerName));
        ProviderKey = providerKey;
        TenantId = tenantId;
    }
}
